#include "rgblcd.h"


void init(void)
{
	GPIO_Init(SPI_CS_PORT, SPI_CS_PIN,  1, 0, 0);
	GPIO_Init(SPI_SCLK_PORT, SPI_SCLK_PIN,  1, 0, 0);
	GPIO_Init(SPI_SDA_PORT, SPI_SDA_PIN, 1, 0, 0);
	
}

void spi_write(uint8_t data,uint8_t mode)
{
	uint16_t  dat = 0,i;
	dat = data;
	if(mode == 1){
		dat |= 0x100;
		
	}
	for(i = 0; i< 9;i++){
		((dat << i) & 0x100)?SPI_SDA_H:SPI_SDA_L;
		SPI_SCLK_L;
		SPI_SCLK_H;
	}
}


static void SPI_WriteComm(uint8_t cmd)
{
	spi_write(cmd,0);
}


static void SPI_WriteData(uint8_t data)
{
	spi_write(data,1);
}

void lcd_memory_init(void)
{
	SDRAM_InitStructure SDRAM_InitStruct;
			
	PORT->PORTP_SEL0 = 0xAAAAAAAA; //PP0-23 => ADDR0-23
//    PORT->PORTP_SEL1 = 0x00000A0A;	//此处需要调整P23\P22\P19\P18的端口，这几个端口用作SpiFlash的通讯，其中P22是片选，片选作为普通IO端口应用。
																		//如此处没有注意，先初始化SpiFlash，再初始化SDRAM的情况下；或应用中disable SDRAM,再进行开启时，都会把SpiFlash的端口状态进行重置。
	PORT->PORTP_SEL1 = 0x00004A5A;		

	PORT->PORTM_SEL0 = 0xAAAAAAAA; //PM0-15 => DATA15-0
	PORT->PORTM_INEN |= 0xFFFF;
	//PM16 => OEN,PM17 => WEN,PM18 => NORFL_CSN,PM19 => SDRAM_CSN,PM20 => SRAM_CSN,PM21 => SDRAM_CKE
	PORT->PORTM_SEL1 = 0x888;

	SDRAM_InitStruct.CellSize = SDRAM_CELLSIZE_64Mb;
	SDRAM_InitStruct.CellBank = SDRAM_CELLBANK_4;
	SDRAM_InitStruct.CellWidth = SDRAM_CELLWIDTH_16;
	SDRAM_InitStruct.CASLatency = SDRAM_CASLATENCY_2;
	SDRAM_InitStruct.TimeTMRD = SDRAM_TMRD_3;
	SDRAM_InitStruct.TimeTRRD = SDRAM_TRRD_2;
	SDRAM_InitStruct.TimeTRAS = SDRAM_TRAS_6;
	SDRAM_InitStruct.TimeTRC  = SDRAM_TRC_8;
	SDRAM_InitStruct.TimeTRCD = SDRAM_TRCD_3;
	SDRAM_InitStruct.TimeTRP  = SDRAM_TRP_3;
	SDRAM_Init(&SDRAM_InitStruct);
}



void lcd_rgb_init(void)
{
	LCD_InitStructure LCD_initStruct;
	
	GPIO_Init(LCD_RESET_PORT, LCD_RESET_PIN, 1, 0, 0);														//复位
  GPIO_Init(LCD_BL_PORT, LCD_BL_PIN, 1, 0, 0);																	//背光
	LCD_RESET_H;																																	//复位拉高
	LCD_BL_L;																																			//背光关闭

	PORT->PORTN_SEL0 = 0xAAAAAAAA;   //GPION.0~15  LCD_DATA0~15
	PORT->PORTN_SEL1 = 0xAA;

	LCD_initStruct.Interface = LCD_INTERFACE_RGB;
	LCD_initStruct.HnPixel = LV_HOR_RES_MAX;
	LCD_initStruct.VnPixel = LV_VER_RES_MAX;
	LCD_initStruct.Hfp = 10;
	LCD_initStruct.Hbp = 20;
	LCD_initStruct.Vfp = 10;
	LCD_initStruct.Vbp = 10;
	LCD_initStruct.ClkDiv = LCD_CLKDIV_4;
	LCD_initStruct.ClkAlways = 0;
	LCD_initStruct.SamplEdge = LCD_SAMPLEDGE_RISE;
	LCD_initStruct.HsyncWidth = LCD_HSYNC_1DOTCLK;
	LCD_initStruct.IntEOTEn = 1;
	
	LCD_Init(LCD, &LCD_initStruct);
	init();//SPI1初始化
	
	LCD_RESET_H;
	swm_delay(20);
  LCD_RESET_L;//复位拉低
	swm_delay(20);
	LCD_RESET_H;//复位拉高

	//PAGE1
	SPI_CS_L;
	SPI_WriteComm(0xFF);    
	SPI_WriteData(0x77);
	SPI_WriteData(0x01);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x10);

	SPI_WriteComm(0xC0);    
	SPI_WriteData(0x3B);
	SPI_WriteData(0x00);

	SPI_WriteComm(0xC1);    
	SPI_WriteData(0x0D);
	SPI_WriteData(0x02);

	SPI_WriteComm(0xC2);
	SPI_WriteData(0x31);
	SPI_WriteData(0x05);

//---------------------------

//	SPI_WriteComm(0xC3);
//	SPI_WriteData(0x80);
//	SPI_WriteData(0x14);
//	SPI_WriteData(0x0A);

	SPI_WriteComm(0xCd);
	SPI_WriteData(0x08);

	SPI_WriteComm(0xB0);    
	SPI_WriteData(0x00); //Positive Voltage Gamma Control
	SPI_WriteData(0x11);
	SPI_WriteData(0x18);
	SPI_WriteData(0x0E);
	SPI_WriteData(0x11);
	SPI_WriteData(0x06);
	SPI_WriteData(0x07);
	SPI_WriteData(0x08);
	SPI_WriteData(0x07);
	SPI_WriteData(0x22);
	SPI_WriteData(0x04);
	SPI_WriteData(0x12);
	SPI_WriteData(0x0F);
	SPI_WriteData(0xAA);
	SPI_WriteData(0x31);
	SPI_WriteData(0x18);


	SPI_WriteComm(0xB1);    
	SPI_WriteData(0x00); //Negative Voltage Gamma Control
	SPI_WriteData(0x11);
	SPI_WriteData(0x19);
	SPI_WriteData(0x0E);
	SPI_WriteData(0x12);
	SPI_WriteData(0x07);
	SPI_WriteData(0x08);
	SPI_WriteData(0x08);
	SPI_WriteData(0x08);
	SPI_WriteData(0x22);
	SPI_WriteData(0x04);
	SPI_WriteData(0x11);
	SPI_WriteData(0x11);
	SPI_WriteData(0xA9);
	SPI_WriteData(0x32);
	SPI_WriteData(0x18);

	//PAGE1
	SPI_WriteComm(0xFF);    
	SPI_WriteData(0x77);
	SPI_WriteData(0x01);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x11);


	SPI_WriteComm(0xB0);    SPI_WriteData(0x60); //Vop=4.7375v

	SPI_WriteComm(0xB1);    SPI_WriteData(0x32); //VCOM=32

	SPI_WriteComm(0xB2);    SPI_WriteData(0x07); //VGH=15v

	SPI_WriteComm(0xB3);    SPI_WriteData(0x80);

	SPI_WriteComm(0xB5);    SPI_WriteData(0x49); //VGL=-10.17v

	SPI_WriteComm(0xB7);    SPI_WriteData(0x85);

	SPI_WriteComm(0xB8);    SPI_WriteData(0x21); //AVDD=6.6 & AVCL=-4.6

	SPI_WriteComm(0xC1);    SPI_WriteData(0x78);

	SPI_WriteComm(0xC2);    SPI_WriteData(0x78);

	SPI_WriteComm(0xE0);    
	SPI_WriteData(0x00);
	SPI_WriteData(0x1B);
	SPI_WriteData(0x02);

	SPI_WriteComm(0xE1);   
	SPI_WriteData(0x08);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x07);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x44);
	SPI_WriteData(0x44);



	SPI_WriteComm(0xE2);    
	SPI_WriteData(0x11);
	SPI_WriteData(0x11);
	SPI_WriteData(0x44);
	SPI_WriteData(0x44);
	SPI_WriteData(0xED);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0xEC);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);


	SPI_WriteComm(0xE3);    
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x11);
	SPI_WriteData(0x11);


	SPI_WriteComm(0xE4);    
	SPI_WriteData(0x44);
	SPI_WriteData(0x44);

	SPI_WriteComm(0xE5);    
	SPI_WriteData(0x0A);
	SPI_WriteData(0xE9);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x0C);
	SPI_WriteData(0xEB);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x0E);
	SPI_WriteData(0xED);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x10);
	SPI_WriteData(0xEF);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);




	SPI_WriteComm(0xE6);   
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x11);
	SPI_WriteData(0x11);

	SPI_WriteComm(0xE7);    
	SPI_WriteData(0x44);
	SPI_WriteData(0x44);


	SPI_WriteComm(0xE8);    
	SPI_WriteData(0x09);
	SPI_WriteData(0xE8);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x0B);
	SPI_WriteData(0xEA);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x0D);
	SPI_WriteData(0xEC);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);
	SPI_WriteData(0x0F);
	SPI_WriteData(0xEE);
	SPI_WriteData(0xD8);
	SPI_WriteData(0xA0);


	SPI_WriteComm(0xEB);    
	SPI_WriteData(0x02);
	SPI_WriteData(0x00);
	SPI_WriteData(0xE4);
	SPI_WriteData(0xE4);
	SPI_WriteData(0x88);
	SPI_WriteData(0x00);
	SPI_WriteData(0x40);


	SPI_WriteComm(0xEC);    
	SPI_WriteData(0x3C);
	SPI_WriteData(0x00);


	SPI_WriteComm(0xED);    
	SPI_WriteData(0xAB);
	SPI_WriteData(0x89);
	SPI_WriteData(0x76);
	SPI_WriteData(0x54);
	SPI_WriteData(0x02);
	SPI_WriteData(0xFF);
	SPI_WriteData(0xFF);
	SPI_WriteData(0xFF);
	SPI_WriteData(0xFF);
	SPI_WriteData(0xFF);
	SPI_WriteData(0xFF);
	SPI_WriteData(0x20);
	SPI_WriteData(0x45);
	SPI_WriteData(0x67);
	SPI_WriteData(0x98);
	SPI_WriteData(0xBA);

	SPI_WriteComm(0x36);    
	SPI_WriteData(0x00);


	//-----------VAP & VAN---------------
	SPI_WriteComm(0xFF);    
	SPI_WriteData(0x77);
	SPI_WriteData(0x01);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x13);

	SPI_WriteComm(0xE5);    
	SPI_WriteData(0xE4);

	SPI_WriteComm(0xFF);
	SPI_WriteData(0x77);
	SPI_WriteData(0x01);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);
	SPI_WriteData(0x00);

	SPI_WriteComm(0x3a);
	SPI_WriteData(0x66);
	SPI_WriteComm(0x21);
	swm_delay(120);
	SPI_WriteComm(0x11);
	swm_delay(120);
	SPI_WriteComm(0x29); 
	SPI_CS_H;
//	LCD_BL_H;//背光打开
}

void LCD_Handler(void)
{
    LCD_INTClr(LCD);
    LCD_Start(LCD);
}
